<template>
	<view>
		<view class="bg-white" style="width: 100%; height: 100rpx;display: flex;">
			<view class="" style="width: 620rpx;">
				
			
				<u-search height="76" shape="squares" :placeholder="placeholdertext" v-model="keyword"></u-search>
				</view>
				<view class="lg flex-content" style="margin-right: 60rpx;" @click="searchfn">
					搜索
				</view>
		</view>
		<view class="md m-l-40" style="color: #959494;" v-if="isresult">
			{{tipcontent}}
		</view>
		<mescroll-body v-if="havecontent" ref="mescrollRef" @init="mescrollInit" @up="upCallback" :down="downOption"
			:up="upOption" @down="downCallback">
		<view class="" v-for="(item,index) in list" :key="index">
	
			<view class="p-20"  v-if="searchtype==1">
				<search-result :item="item"></search-result>
			</view>
			<view class="p-20" v-if="searchtype==2">
			<search-list  :item="item" ></search-list>
			</view>
		</view>
		</mescroll-body>
		
	</view>
</template>

<script>
	import {getSearchcollectdr,getSearchPhoner} from "@/api/user"
	import {searchDriver} from "@/api/car"
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				isresult:false,
				keyword:"",
				tipcontent:"以下司机为搜索结果，收藏可以直接发送订单",
				placeholdertext:"",
				list: [],
				downOption: {
					use: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				havecontent: false,
				search:"",
				mobile:"",
				upOption: {},
				searchtype:0,
				searchpages:{
					page:1,
					limit:10
				}
			}
		},
		onLoad(opt) {
			this.searchtype=opt.type
			if(opt.type==1){
				uni.setNavigationBarTitle({
					title:"添加收藏司机"
				})
				this.placeholdertext="请输入手机号"
			}else if(opt.type==2){
				uni.setNavigationBarTitle({
					title:"查找司机"
				})
				this.placeholdertext="如司机姓名、车型等"
			}
		},
		methods: {
			searchfn(){
				
				if(this.keyword==''&&this.searchtype==1){
					return this.$toast({
					title:"请填写搜索手机号"
					})
					
				}else if(this.keyword==''&&this.searchtype==2){
					return this.$toast({
							title:"请填写搜索关键字"
						
					})
				}
				this.list=[]
				this.isresult=true
				this.havecontent=true
				if(this.searchtype==1){
					this.searchphone(1,10,this.keyword)
				}else if(this.searchtype==2){
					this.serchText(1,10,this.keyword)
				}
			},
			upCallback(page) {
				this.isresult=true
				
				// this.havecontent=true
				const pageNum = page.num; // 页码, 默认从1开始
				const pageSize = page.size; 
				this.mescroll.endBySize(1, 1);
				// 页长, 默认每页10条
				if(this.searchtype==1){
					if(pageNum==1){
						return
					}
					this.searchphone(pageNum,page,this.keyword)
				}else
					if(this.searchtype==2){
						if(pageNum==1){
							return
						}
						this.serchText(pageNum,page,this.keyword)
				
				}
				
			},
			searchphone(page,limit,search){
				getSearchPhoner({
					mobile:search,
				
					
				}).then(({
					code,
					data
				}) => {
					if (code == 1) {
						if (page.num == 1) this.list = [];
						// console.log(page.num);
						const curPageData = data.lists;
						const curPageLen = curPageData.length;
						const totalSize=data.count
						if(curPageData.length==0){this.tipcontent="暂无该司机"}
						const hasNext = !!data.more;
						this.list = this.list.concat(curPageData);
						console.log(this.list);
						if (this.list.length != 0) {
							this.havecontent = true
				
						}
						console.log(curPageLen,totalSize);
						this.mescroll.endSuccess(curPageLen, hasNext);
						this.mescroll.endBySize(curPageLen, totalSize);
					}
				
				}).catch(err => {
					console.log(err);
					// this.mescroll.endErr()
				})
			},
			serchText(page,limit,search){
				getSearchcollectdr({
					search:search,

					page: page,
					limit: limit,
				}).then(({
					code,
					data
				}) => {
					if (code == 1) {
						if (page.num == 1) this.list = [];
						// console.log(page.num);
						const curPageData = data.lists;
						const curPageLen = curPageData.length;
						if(curPageData.length==0){this.tipcontent="暂无该司机"}
						const totalSize=data.count
						const hasNext = !!data.more;
						this.list = this.list.concat(curPageData);
						if (this.list.length == 0) {
							this.havecontent = true
				
						}
						
						console.log(this.list);
							console.log(curPageLen,totalSize);
						// this.mescroll.endSuccess(curPageLen, hasNext);
						this.mescroll.endBySize(curPageLen, totalSize);
					}
				
				}).catch(err => {
					console.log(err);
					// this.mescroll.endErr()
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.p-20{
	padding: 20rpx;
}
</style>
